﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Management;
using System.Security.AccessControl;
using autobackup.util;

namespace TestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
        }

        public static UInt32 Create(string Description, UInt32 MaximumAllowed, string Name, string Path)
        {
            ManagementBaseObject inParams = null;
            ManagementClass classObj = new System.Management.ManagementClass(null, "Win32_Share", null);
            inParams = classObj.GetMethodParameters("Create");
            inParams["Access"] = null; // default Security descriptor used
            inParams["Description"] = Description;
            inParams["Name"] = Name;
            inParams["Path"] = Path;
            inParams["Type"] = 0; // Disk drive 0, print queue 1
            ManagementBaseObject outParams = classObj.InvokeMethod("Create", inParams, null);
            return Convert.ToUInt32(outParams.Properties["ReturnValue"].Value);
        }
        ///删除共享
        public static UInt32 Delete(ManagementObject share)
        {
            ManagementBaseObject inParams = null;
            ManagementBaseObject outParams = share.InvokeMethod("Delete", inParams, null);
            return System.Convert.ToUInt32(outParams.Properties["ReturnValue"].Value);
        }

        [TestMethod]
        public void testCreate()
        {
            string sharename = "智达代码";
            string path = @"D:\jiahui\project";

            //删除
            /*
            ManagementObject share = new ManagementObject("Win32_Share=" + "'" + sharename + "'");
            string s = (string)share.Properties["Description"].Value; // read property "description"
            // Console.ReadLine(); // press enter to delete share
            UInt32 ret = Delete(share);
            Console.WriteLine("Deletion returned: " + ret);
            */

            //创建
            UInt32 ret1 = Create("智达备份", 10, sharename, path);
            Console.WriteLine("create returned: " + ret1);

            //设置共享权限 若不设置则创建的共享文件不允许任何访问！ 
            DirectorySecurity ds = new DirectorySecurity();
            ds.AddAccessRule(new FileSystemAccessRule("everyone", FileSystemRights.ReadAndExecute | FileSystemRights.Write, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
            System.IO.Directory.SetAccessControl(path, ds);
        }

        [TestMethod]
        public void testCreate2()
        {
            string sharename = "智达代码";
            string path = @"D:\jiahui\工作项目\压力测试\压力测试方案";
            ShareFolder.Share(path, sharename, "智达备份");

        }
    }
}
